﻿Public Partial Class GL_Popup
    Inherits System.Web.UI.Page

    Dim clsGet As New ClassFunctionGetData
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            'Dim lbUser As Label = CType(Master.FindControl("lbMenu"), Label)
            'lbUser.Text = "GL Account Popup"
            Dim ds As DataTable = clsGet.Get_Bank("")
            If (ds.Rows.Count > 0) Then
                With ddlBank
                    .ClearSelection()
                    .DataTextField = "Bank_Name"
                    .DataValueField = "Bank_SAP"
                    .DataSource = ds
                    .DataBind()
                    .Items.Insert(0, "<--ทั้งหมด-->")
                    .Items(0).Value = "0"
                End With
            End If
            If (Not (Session("GL") Is Nothing)) Then
                txtGL_account.Text = Session("GL").ToString.Split("$")(0)
                txtGL_account.Enabled = False
                txtName.Text = Session("GL").ToString.Split("$")(1)
                ViewState("GL") = Session("GL").ToString.Split("$")(0)
                If Not (ddlBank.Items.FindByValue(Session("GL").ToString.Split("$")(2)) Is Nothing) Then
                    ddlBank.SelectedValue = Session("GL").ToString.Split("$")(2)
                End If
            Else
                ViewState("GL") = ""
            End If
            If (Session("EditMaster") Is Nothing) Then
                btnSave.Visible = False
            End If
        End If
    End Sub

    Private Sub btnCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Session("GL") = Nothing
        ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " parent.closeModal();", True)
    End Sub

    Private Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        If (ddlBank.SelectedValue = "0") Then
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('กรุณาทำการระบุธนาคาร');", True)
            Exit Sub
        End If
        If (txtGL_account.Text.Trim() = "") Then
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('กรุณาทำการระบุ GL Account');", True)
            Exit Sub
        End If
        If (txtName.Text.Trim() = "") Then
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('กรุณาทำการระบุชื่อบัญชี');", True)
            Exit Sub
        End If
        Dim conn As New System.Data.SqlClient.SqlConnection(connString)
        Dim clsDB As New ClassDatabase
        Dim clsFc As New ClassFunction
        Dim condition As Boolean = True
        Dim tran As System.Data.SqlClient.SqlTransaction = Nothing
        Application.Lock()
        conn.Open()
        Dim Line_Code As String = ""
        If ViewState("GL").ToString() = "" Then
            Line_Code = txtGL_account.Text
        Else
            Line_Code = ViewState("GL").ToString()
        End If

        '  tran = clsDB.BeginTran(conn)
        If (clsFc.InsertGL_Account(ViewState("GL").ToString(), Line_Code, txtName.Text, ddlBank.SelectedValue, conn, tran) > 0) Then
            Session("GL") = Nothing
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "parent.closeModal();alert('บันทึกข้อมูลเสร็จสมบูรณ์ !!');", True)
        Else
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "alert('เกิดความผิดพลาดในการบันทึกข้อมูล !!');", True)
        End If
        conn.Close()
        Application.UnLock()
        conn.Dispose()
    End Sub

    Private Sub MainPage_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit
        Dim ds As DataSet = Session("UserData")
        If Not (ds Is Nothing) Then
            Page.MasterPageFile = clsGet.GetMaster_Page(ds.Tables(0).Rows(0).Item("User_Name").ToString(), "")
        End If
    End Sub
End Class